package com.taijihuabao.modules.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taijihuabao.modules.entity.MenuEntity;
import com.taijihuabao.modules.mapper.MenuMapper;
import com.taijihuabao.modules.model.menu.dto.MenuCrFromDTO;
import com.taijihuabao.modules.model.menu.dto.MenuListFromDTO;
import com.taijihuabao.modules.model.menu.dto.MenuUpFromDTO;
import com.taijihuabao.modules.model.menu.vo.MenuInfoVO;
import com.taijihuabao.modules.service.MenuService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单管理
 *
 * @author Wang
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    MenuMapper menuMapper;

    @Override
    public PageInfo selectMenus(MenuListFromDTO listFromDTO) {
        PageHelper.startPage(listFromDTO.getPage(), listFromDTO.getSize());
        MenuEntity menuEntity = new MenuEntity();
        menuEntity.setParentId(listFromDTO.getParentId());
        menuEntity.setName(listFromDTO.getName());
        menuEntity.setMenuUrl(listFromDTO.getMenuUrl());
        menuEntity.setMenuLevel(listFromDTO.getMenuLevel());
        List<MenuEntity> menuEntities = menuMapper.selectMenus(menuEntity);
        PageInfo pageInfo = new PageInfo(menuEntities);
        return pageInfo;
    }

    @Override
    public MenuInfoVO selectById(Long id) {
        MenuInfoVO menuInfoVO = menuMapper.selectById(id);
        return menuInfoVO;
    }

    @Override
    public List<MenuEntity> selectParent(Long id) {
        List<MenuEntity> menuEntityList = new ArrayList<>();
        List<MenuEntity> parentEntity = getParentEntity(id, menuEntityList);
        return parentEntity;
    }

    public List<MenuEntity> getParentEntity(Long id, List<MenuEntity> menuEntityList) {
        MenuEntity menuEntity = menuMapper.selectOneById(id);
        MenuEntity parentMenuEntity = menuMapper.selectOneById(menuEntity.getParentId());
        if (menuEntity.getParentId() != 0) {
            menuEntityList.add(parentMenuEntity);
            getParentEntity(menuEntity.getParentId(), menuEntityList);
        }
        return menuEntityList;
    }

    @Override
    public List<MenuEntity> selectAllMenus() {
        List<MenuEntity> menuEntities = menuMapper.selectParentMenus();
        return menuEntities;
    }

    @Override
    public void insertMenu(MenuCrFromDTO crFromDTO) {
        MenuEntity menuEntity = new MenuEntity();
        //新增菜单信息
        menuEntity.setParentId(crFromDTO.getParentId());
        menuEntity.setName(crFromDTO.getName());
        menuEntity.setMenuUrl(crFromDTO.getMenuUrl());
        menuEntity.setMenuLevel(crFromDTO.getMenuLevel());
        menuMapper.insertMenu(menuEntity);
    }

    @Override
    public void updateMenu(MenuUpFromDTO upFromDTO) {
        MenuEntity menuEntity = new MenuEntity();
        //修改菜单信息
        menuEntity.setId(upFromDTO.getId());
        menuEntity.setParentId(upFromDTO.getParentId());
        menuEntity.setName(upFromDTO.getName());
        menuEntity.setMenuUrl(upFromDTO.getMenuUrl());
        menuEntity.setMenuLevel(upFromDTO.getMenuLevel());
        menuMapper.updateMenu(menuEntity);
    }

    @Override
    public void deleteMenu(Long id) {
        //拿到当前菜单的信息
        MenuInfoVO menuInfoVO = menuMapper.selectById(id);
        //拿到当前菜单的子级菜单集合
        List<MenuInfoVO> sonMenus = menuInfoVO.getSonMenus();
        //当前菜单没有子级菜单了就停止递归
        if (!CollectionUtils.isEmpty(sonMenus)){
            for (MenuInfoVO sonMenuInfoVO : sonMenus) {
                //拿到当前子级菜单的子级菜单
                deleteMenu(sonMenuInfoVO.getId());
            }
        }
        MenuEntity menuEntity = new MenuEntity();
        menuEntity.setId(id);
        menuMapper.deleteMenu(menuEntity);
    }

}

